home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / tp5tsr.zip / TSRUNIT.PAS < prev   
Pascal/Delphi Source File  |  1989-05-27  |  29KB  |  572 lines

  1. UNIT TSRUnit; {Create TSR programs with Turbo Pascal 5.0 & TSRUnit}
  2.  
  3. {$B-,F-,I+,R-,S+} {Set compiler directives to normal values.}
  4.  
  5. INTERFACE {=======================================================}
  6. {
  7. The author and any distributor of this software assume no responsi-
  8. bility for damages resulting from this software or its use due to
  9. errors, omissions, incompatibility with other software or with
  10. hardware, or misuse; and specifically disclaim any implied warranty
  11. of fitness for any particular purpose or application.
  12. }
  13. USES DOS, CRT;
  14. CONST
  15. {*** Shift key combination codes.                                 }
  16.   AltKey = 8;  CtrlKey = 4;  LeftKey = 2;  RightKey = 1;
  17.  
  18.   TSRVersion : WORD = $0204;       {Low byte.High byte = 2.04     }
  19.  
  20. TYPE
  21.   String80  = STRING[80];
  22.   ChrWords  = RECORD CASE INTEGER OF
  23.                   1: ( W: WORD );
  24.                   2: ( C: CHAR; A: BYTE );
  25.               END;
  26.   LineWords = ARRAY[1..80] OF ChrWords;
  27.   WordFuncs = FUNCTION : WORD;
  28.  
  29. VAR
  30.   TSRScrPtr : POINTER; {Pointer to saved screen image.            }
  31.   TSRChrPtr : POINTER; {Pointer to first character to insert.     }
  32.   TSRMode   : BYTE;    {Video mode --------- before TSR popped up.}
  33.   TSRWidth  : BYTE;    {Number of screen columns-- " "    "    " .}
  34.   TSRPage   : BYTE;    {Active video page number-- " "    "    " .}
  35.   TSRColumn : BYTE;    {Cursor column number ----- " "    "    " .}
  36.   TSRRow    : BYTE;    {Cursor row number -------- " "    "    " .}
  37. {
  38. ** Procedure for installing the TSR program.                      }
  39. PROCEDURE TSRInstall( TSRName : STRING;   {Name or title for TSR. }
  40.                       TSRFunc : WordFuncs;{Ptr to FUNCTION to call}
  41.                       ShiftComb: BYTE;    {Hot key--shift key comb}
  42.                       KeyChr   : CHAR );  {Hot Key--character key.}
  43. {
  44.   ShiftComb and KeyChr specify the default hot keys for the TSR.
  45.   ShiftComb may be created by adding or ORing the constants AltKey,
  46.   CtrlKey, LeftKey, and RightKey together.  KeyChr may be
  47.   characters 0-9 and A-Z.
  48.  
  49.   The default hot keys may be overridden when the TSR is installed
  50.   by specifying optional parameters on the command line.  The
  51.   parameter format is:
  52.                        [/A] [/C] [/R] [/L] [/"[K["]]]
  53.   The square brackets surround optional items--do not include them.
  54.   Any characters between parameters are ignored. The order of the
  55.   characters does not matter; however, the shift keys specified are
  56.   cummulative and the last character key "K" specified is the used.
  57. }
  58. {
  59. ** Functions for checking status of printer LPT1.                 }
  60. FUNCTION PrinterOkay:   BOOLEAN; {Returns TRUE if printer is okay.}
  61. FUNCTION PrinterStatus: BYTE;    {Returns status of printer.
  62.   Definition of status byte bits (1 & 2 are not used), if set then:
  63.  Bit: -- 7 ---  ---- 6 ----  -- 5 ---  -- 4 ---  -- 3 --  --- 0 ---
  64.       Not busy  Acknowledge  No paper  Selected  I/O Err. Timed-out
  65. }
  66. {
  67. ** Routines for obtaining one row of screen characters.           }
  68. FUNCTION ScreenLineStr( Row: BYTE ): String80; {Returns char. str.}
  69. PROCEDURE ScreenLine( Row: BYTE; VAR Line: LineWords; {Returns    }
  70.                                  VAR Words: BYTE );   {chr & color}
  71.  
  72. IMPLEMENTATION {==================================================}
  73. VAR
  74.   BuffSize, InitCMode : WORD;
  75.   NpxFlag             : BOOLEAN;
  76.   Buffer              : ARRAY[0..8191] OF WORD;
  77.   NpxState            : ARRAY[0..93] OF BYTE;
  78.   RetrnVal, InitVideo : BYTE;
  79.   TheirFunc           : WordFuncs;
  80.  
  81. CONST    {Offsets to items contained in PROCEDURE Asm.            }
  82.   UnSafe = 0;    Flg   = 1;     Key     = 2;     Shft  = 3;
  83.   StkOfs = 4;    StkSs = 6;     DosSp   = 8;     DosSs = 10;
  84.   Prev  = 12;    Flg9  = 13;    InsNumb = 14;
  85.   Dos21 = $10;         Dos25  = Dos21+4;      Dos26  = Dos25+4;
  86.   Bios9 = Dos26+4;     Bios16 = Bios9+4;      DosTab = Bios16+4;
  87.   Our21 = DosTab+99;   Our25  = Our21+51;     Our26  = Our25+27;
  88.   Our09 = Our26+27;    Our16  = Our09+127+8;  InsChr = Our16+180-8;
  89.   PopUp = InsChr+4;
  90.  
  91. PROCEDURE Asm; {Inline code--data storage and intercept routines. }
  92. INTERRUPT;
  93. BEGIN
  94. INLINE(
  95. {***  Storage for interrupt vectors.                              }
  96.       {Dos21:  }  >0/>0/    {DOS func. intr vector.               }
  97.       {Dos25:  }  >0/>0/    {DOS abs. disk read intr. vector.     }
  98.       {Dos26:  }  >0/>0/    {DOS abs. sector write intr.vector.   }
  99.       {Bios9:  }  >0/>0/    {BIOS key stroke intr. vector.        }
  100.       {Bios16: }  >0/>0/    {BIOS buffered keybd. input intr.vect.}
  101.  
  102.       {DosTab: ARRAY[0..98] OF BYTE = {Non-reetrant DOS functions.}
  103.       0/0/0/0/0/0/0/0/  0/0/0/0/0/1/1/1/  1/1/1/1/1/1/1/1/
  104.       1/1/1/1/1/1/1/1/  1/1/1/1/1/1/0/1/  1/1/1/1/1/1/1/0/
  105.       1/0/0/0/0/0/1/1/  1/1/1/1/1/1/1/1/  1/1/1/1/1/1/1/1/
  106.       0/0/0/0/0/0/1/1/  0/0/0/0/1/0/1/1/  0/1/1/1/1/0/0/0/  0/0/0/
  107.  
  108. {*** OurIntr21 ******* Intercept routine for DOS Function Intr.***}
  109. {  0} $9C/               { PUSHF            ;Save flags.          }
  110. {  1} $FB/               { STI              ;Enable interrupts.   }
  111. {  2} $80/$FC/$63/       { CMP  AH,63H      ;Assume unsafe if new }
  112. {  5} $73/<22-7/         { JNB  IncF        ;function--skip table.}
  113. {  7} $50/               { PUSH AX          ;Save registers.      }
  114. {  8} $53/               { PUSH BX          ;Load offset to table.}
  115. {  9} $BB/>DosTab/       { MOV  BX,[DosTab]                       }
  116. { 12} $8A/$C4/           { MOV  AL,AH       ;Load table entry     }
  117. { 14} $2E/               { CS:              ;index.               }
  118. { 15} $D7/               { XLAT             ;Get value from table.}
  119. { 16} $3C/$00/           { CMP  AL,0        ;If TRUE then set flag}
  120. { 18} $5B/               { POP  BX          ;Restore registers.   }
  121. { 19} $58/               { POP  AX          ;                     }
  122. { 20} $74/$17/           { JZ   JmpDos21    ;Jump to orig. intr.  }
  123. { 22} $2E/          {IncF: CS:              ;                     }
  124. { 23} $FE/$06/>UnSafe/   { INC  [UnSafe]    ;Set UnSafe flag.     }
  125. { 27} $9D/               { POPF             ;Restore flags.       }
  126. { 28} $9C/               { PUSHF            ;                     }
  127. { 29} $2E/               { CS:              ;                     }
  128. { 30} $FF/$1E/>Dos21/    { CALL FAR [Dos21] ;Call orig. intr.     }
  129. { 34} $FB/               { STI              ;Enable interrupts.   }
  130. { 35} $9C/               { PUSHF            ;Save flags.          }
  131. { 36} $2E/               { CS:              ;                     }
  132. { 37} $FE/$0E/>UnSafe/   { DEC  [UnSafe]    ;Clear UnSafe flag.   }
  133. { 41} $9D/               { POPF             ;Restore flags.       }
  134. { 42} $CA/$02/$00/       { RETF 2           ;Return & remove flag.}
  135.  
  136. { 45} $9D/      {JmpDos21: POPF             ;Restore flags.       }
  137. { 46} $2E/               { CS:              ;                     }
  138. { 47} $FF/$2E/>Dos21/    { JMP FAR [Dos21]  ;Jump to orig. intr.  }
  139. { 51}
  140. {*** OurIntr25 ********** Intercept routine for DOS Abs. Read *** }
  141. {  0} $9C/               { PUSHF            ;Save flags.          }
  142. {  1} $2E/               { CS:              ;                     }
  143. {  2} $FE/$06/>UnSafe/   { INC  [UnSafe]    ;Set UnSafe flag.     }
  144. {  6} $9D/               { POPF             ;Restore flags.       }
  145. {  7} $9C/               { PUSHF            ;                     }
  146. {  8} $2E/               { CS:              ;                     }
  147. {  9} $FF/$1E/>Dos25/    { CALL FAR [Dos25] ;Call DOS abs. read.  }
  148. { 13} $68/>Our25+19/     { PUSH Our25+19    ;Clean up stack with- }
  149. { 16} $C2/$02/$00/       { RET  2           ;out changing flags.  }
  150. { 19} $9C/               { PUSHF            ;Save flags.          }
  151. { 20} $2E/               { CS:              ;                     }
  152. { 21} $FE/$0E/>UnSafe/   { DEC  [UnSafe]    ;Clear UnSafe flag.   }
  153. { 25} $9D/               { POPF             ;Restore flags.  Leave}
  154. { 26} $CB/               { RETF             ;old flags on the stk.}
  155. { 27}
  156. {*** OurIntr26 ********** Intercept routine for DOS Abs. Write ***}
  157. {  0} $9C/               { PUSHF            ;Save flags.          }
  158. {  1} $2E/               { CS:              ;                     }
  159. {  2} $FE/$06/>UnSafe/   { INC  [UnSafe]    ;Set UnSafe flag.     }
  160. {  6} $9D/               { POPF             ;Restore flags.       }
  161. {  7} $9C/               { PUSHF            ;                     }
  162. {  8} $2E/               { CS:              ;                     }
  163. {  9} $FF/$1E/>Dos26/    { CALL FAR [Dos26] ;Call DOS abs. write. }
  164. { 13} $68/>Our26+19/     { PUSH Our26+19    ;Clean up stack with- }
  165. { 16} $C2/$02/$00/       { RET  2           ;out changing flags.  }
  166. { 19} $9C/               { PUSHF            ;Save flags.          }
  167. { 20} $2E/               { CS:              ;                     }
  168. { 21} $FE/$0E/>UnSafe/   { DEC  [UnSafe]    ;Clear UnSafe flag.   }
  169. { 25} $9D/               { POPF             ;Restore flags.  Leave}
  170. { 26} $CB/               { RETF             ;old flags on the stk.}
  171. { 27}
  172.  
  173. {*** OurIntr9 ********** Intercept for BIOS Hardware Keyboard Intr}
  174. {  0} $9C/               { PUSHF            ;Entry point.         }
  175. {  1} $FB/               { STI              ;Enable interrupts.   }
  176. {  2} $1E/               { PUSH DS          ;                     }
  177. {  3} $0E/               { PUSH CS          ;DS := CS;            }
  178. {  4} $1F/               { POP  DS          ;                     }
  179. {  5} $50/               { PUSH AX          ;Preserve AX on stack.}
  180. {  6} $31/$C0/           { XOR  AX,AX       ;Set AH to 0.         }
  181. {  8} $E4/$60/           { IN   AL,60h      ;Read byte from keybd }
  182. { 10} $3C/$E0/           { CMP  AL,0E0h     ;If multi-byte codes, }
  183. { 12} $74/<75-14/        { JE   Sfx         ;then jump and set    }
  184. { 14} $3C/$F0/           { CMP  AL,0F0h     ;multi-byte flag, Flg9}
  185. { 16} $74/<75-18/        { JE   Sfx         ;                     }
  186. { 18} $80/$3E/>Flg9/$00/ { CMP  [Flg9],0    ;Exit if part of      }
  187. { 23} $75/<77-25/        { JNZ  Cfx         ;multi-byte code.     }
  188. { 25} $3A/$06/>Key/      { CMP  AL,[Key]    ;Exit if key pressed  }
  189. { 29} $75/<88-31/        { JNE  PreExit     ;is not hot key.      }
  190.  
  191. { 31} $50/               { PUSH AX          ;Hot key was pressed, }
  192. { 32} $06/               { PUSH ES          ;check shift key      }
  193. { 33} $B8/$40/$00/       { MOV  AX,0040h    ;status byte.  First  }
  194. { 36} $8E/$C0/           { MOV  ES,AX       ;load BIOS segment.   }
  195. { 38} $26/               { ES:              ;                     }
  196. { 39} $A0/>$0017/        { MOV  AL,[0017h]  ;AL:= Shift key status}
  197. { 42} $07/               { POP  ES          ;Restore ES register. }
  198. { 43} $24/$0F/           { AND  AL,0Fh      ;Clear unwanted bits. }
  199. { 45} $3A/$06/>Shft/     { CMP  AL,[Shft]   ;Exit if not hot key  }
  200. { 49} $58/               { POP  AX          ;shift key combination}
  201. { 50} $75/<88-52/        { JNE  PreExit     ;(Restore AX first).  }
  202.  
  203.                          {                  ;Hot Keys encountered.}
  204. { 52} $3A/$06/>Prev/     { CMP  AL,[Prev]   ;Discard repeated hot }
  205. { 56} $74/<107-58/       { JE   Discard     ;key codes.           }
  206. { 58} $A2/>Prev/         { MOV  [Prev],AL   ;Update Prev.         }
  207. { 61} $F6/$06/>Flg/3/    { TEST [Flg],3     ;If Flg set, keep key }
  208. { 66} $75/<99-68/        { JNZ  JmpBios9    ;& exit to orig. BIOS }
  209. { 68} $80/$0E/>Flg/1/    { OR   [Flg],1     ;9.  Else set flag and}
  210. { 73} $EB/<107-75/       { JMP SHORT Discard;discard key stroke.  }
  211.  
  212. { 75} $B4/$01/       {Sfx: MOV  AH,1        ;Load AH with set flag}
  213. { 77} $88/$26/>Flg9/ {Cfx: MOV  [Flg9],AH   ;Save multi-byte flag.}
  214. { 81} $C6/$06/>Prev/$FF/ { MOV  [Prev],0FFh ;Change prev key byte.}
  215. { 86} $EB/<99-88/        { JMP SHORT JmpBios9                     }
  216.  
  217. { 88} $3C/$FF/   {PreExit: CMP  AL,0FFh     ;Update previous key  }
  218. { 90} $74/<99-92/        { JE   JmpBios9    ;unless key is buffer-}
  219. { 92} $3C/$00/           { CMP  AL,0        ;full code--a 00h     }
  220. { 94} $74/<99-96/        { JZ   JmpBios9    ;0FFh                 }
  221. { 96} $A2/>Prev/         { MOV [Prev],AL    ;Update previous key. }
  222.  
  223. { 99} $58/      {JmpBios9: POP  AX          ;Restore registers and}
  224. {100} $1F/               { POP  DS          ;flags.               }
  225. {101} $9D/               { POPF             ;                     }
  226. {102} $2E/               { CS:              ;                     }
  227. {103} $FF/$2E/>Bios9/    { JMP  [Bios9]     ;Exit to orig. intr 9.}
  228.  
  229. {107} $E4/$61/   {Discard: IN   AL,61h      ;Clear key from buffer}
  230. {109} $8A/$E0/           { MOV  AH,AL       ;by resetting keyboard}
  231. {111} $0C/$80/           { OR   AL,80h      ;port and sending EOI }
  232. {113} $E6/$61/           { OUT  61h,AL      ;to intr. handler     }
  233. {115} $86/$E0/           { XCHG AH,AL       ;telling it that the  }
  234. {117} $E6/$61/           { OUT  61h,AL      ;key has been         }
  235. {119} $B0/$20/           { MOV  AL,20h      ;processed.           }
  236. {121} $E6/$20/           { OUT  20h,AL      ;                     }
  237. {123} $58/               { POP  AX          ;Restore registers and}
  238. {124} $1F/               { POP  DS          ;flags.               }
  239. {125} $9D/               { POPF             ;                     }
  240. {126} $CF/               { IRET             ;Return from interrupt}
  241. {127}
  242.  
  243. {*** OurIntr16 ***** Intercept routine for Buffered Keyboard Input}
  244. {  0} $58/     {JmpBios16: POP  AX          ;Restore AX, DS, and  }
  245. {  1} $1F/               { POP  DS          ;FLAGS registers then }
  246. {  2} $9D/               { POPF             ;exit to orig. BIOS   }
  247. {  3} $2E/               { CS:              ;intr. 16h routine.   }
  248. {  4} $FF/$2E/>Bios16/   { JMP  [Bios16]    ;                     }
  249.  
  250. {  8} $9C/     {OurIntr16: PUSHF            ;Preserve FLAGS.      }
  251. {  9} $FB/               { STI              ;Enable interrupts.   }
  252. { 10} $1E/               { PUSH DS          ;Preserve DS and AX   }
  253. { 11} $50/               { PUSH AX          ;registers.           }
  254. { 12} $0E/               { PUSH CS          ;DS := CS;            }
  255. { 13} $1F/               { POP  DS          ;                     }
  256. { 14} $F6/$C4/$EF/       { TEST AH,EFh      ;Jmp if not read char.}
  257. { 17} $75/<48-19/        { JNZ  C3          ;request.             }
  258.  
  259.                          {*** Intercept loop for Read Key service.}
  260. { 19} $F6/$06/>Flg/1/ {C1: TEST [Flg],1     ;If pop up Flg bit is }
  261. { 24} $74/<29-26/        { JZ   C2          ;set then call INLINE }
  262. { 26} $E8/>122-29/       { CALL ToPopUp     ;pop up routine.      }
  263. { 29} $F6/$06/>Flg/16/{C2: TEST [Flg],10h   ;Jmp if insert flg set}
  264. { 34} $75/<48-36/        { JNZ  C3          ;                     }
  265. { 36} $FE/$C4/           { INC  AH          ;Use orig. BIOS       }
  266. { 38} $9C/               { PUSHF            ;service to check for }
  267. { 39} $FA/               { CLI              ;character ready.     }
  268. { 40} $FF/$1E/>Bios16/   { CALL FAR [Bios16];Disable interrupts.  }
  269. { 44} $58/               { POP  AX          ;Restore AX and save  }
  270. { 45} $50/               { PUSH AX          ;it again.            }
  271. { 46} $74/<19-48/        { JZ   C1          ;Loop until chr. ready}
  272.  
  273. { 48} $F6/$06/>Flg/17/{C3: TEST [Flg],11h   ;Exit if neither bit  }
  274. { 53} $74/<-55/          { JZ   JmpBios16   ;of Flg is set.       }
  275. { 55} $F6/$06/>Flg/$01/  { TEST [Flg],1     ;If pop up Flg bit is }
  276. { 60} $74/<65-62/        { JZ   C4          ;set then call INLINE }
  277. { 62} $E8/>122-65/       { CALL ToPopUp     ;pop up routine.      }
  278. { 65} $F6/$06/>Flg/$10/{C4:TEST [Flg],10h   ;Exit unless have     }
  279. { 70} $74/<-72/          { JZ   JmpBios16   ;characters to insert.}
  280. { 72} $F6/$C4/$EE/       { TEST AH,0EEh     ;If request is not a  }
  281. { 75} $75/<-77/          { JNZ  JmpBios16   ;chr. request, exit.  }
  282.  
  283.                          {*** Insert a character.                 }
  284. { 77} $58/               { POP  AX          ;AX := BIOS service no}
  285. { 78} $53/               { PUSH BX          ;Save BX and ES.      }
  286. { 79} $06/               { PUSH ES          ;                     }
  287. { 80} $C4/$1E/>InsChr/   { LES  BX,[InsChr] ;PTR(ES,BX) := InsChr;}
  288. { 84} $26/               { ES:              ;AL := InsChr^;       }
  289. { 85} $8A/$07/           { MOV  AL,[BX]     ;                     }
  290. { 87} $07/               { POP  ES          ;Restore ES and BX.   }
  291. { 88} $5B/               { POP  BX          ;                     }
  292. { 89} $F6/$C4/$01/       { TEST AH,01h      ;IF AH IN [$01,$11]   }
  293. { 92} $B4/$00/           { MOV  AH,00h      ;   THEN ReportOnly;  }
  294. { 94} $75/<114-96/       { JNZ  ReportOnly  ;Set Scan code to 0.  }
  295. { 96} $FE/$06/>InsChr/   { INC  [InsChr]    ;Inc( InsChr );       }
  296. {100} $FF/$0E/>InsNumb/  { DEC  [InsNumb]   ;Dec( InsNumb );      }
  297. {104} $75/<111-106/      { JNZ  SkipReset   ;IF InsNumb = 0 THEN  }
  298. {106} $80/$26/>Flg/$EF/  { AND  [Flg],0EFh  ; Clear insert chr flg}
  299. {111} $1F/     {SkipReset: POP  DS          ;Restore BX, DS, and  }
  300. {112} $9D/               { POPF             ;FLAGS, then return   }
  301. {113} $CF/               { IRET             ;from interrupt.      }
  302.  
  303. {114} $1F/    {ReportOnly: POP  DS          ;Report char. ready.  }
  304. {115} $9D/               { POPF             ;Restore DS and FLAGS.}
  305. {116} $50/               { PUSH AX          ;Clear zero flag bit  }
  306. {117} $40/               { INC  AX          ;to indicate a        }
  307. {118} $58/               { POP  AX          ;character ready.     }
  308. {119} $CA/>0002/         { RETF 2           ;Exit & discard FLAGS }
  309.  
  310.                          {*** Interface to PopUpCode Routine.     }
  311. {122} $50/       {ToPopUp: PUSH AX          ;Save AX.             }
  312. {123} $FA/               { CLI              ;Disable interrupts.  }
  313. {124} $F6/$06/>UnSafe/$FF/{TEST [UnSafe],0FFh ;IF UnSafe <> 0     }
  314. {129} $75/<177-131/      { JNZ  PP2           ;      THEN Return. }
  315. {131} $A0/>Flg/          { MOV  AL,[Flg]    ;Set in-use bit; clear}
  316. {134} $24/$FE/           { AND  AL,0FEh     ;pop up bit of Flg.   }
  317. {136} $0C/$02/           { OR   AL,2        ;Flg := (Flg AND $FE) }
  318. {138} $A2/>Flg/          { MOV  [Flg],AL    ;        OR 2;        }
  319.                          {                  ;**Switch to our stack}
  320. {141} $A1/>StkOfs/       { MOV  AX,[StkOfs] ;Load top of our stack}
  321. {144} $87/$C4/           { XCHG AX,SP       ;Exchange it with     }
  322. {146} $A3/>DosSp/        { MOV  [DosSp],AX  ;stk.ptr, save old SP.}
  323. {149} $8C/$16/>DosSs/    { MOV  [DosSs],SS  ;Save old SS.         }
  324. {153} $8E/$16/>StkSs/    { MOV  SS,[StkSs]  ;Replace SS with our  }
  325. {157} $FB/               { STI              ;SS. Enable interrupts}
  326.  
  327. {158} $9C/               { PUSHF            ;Interrupt call to pop}
  328. {159} $FF/$1E/>PopUp/    { CALL FAR [PopUp] ;up TSR routine.      }
  329.  
  330. {163} $FA/               { CLI              ;Disable interrupts.  }
  331. {164} $8B/$26/>DosSp/    { MOV  SP,[DosSp]  ;Restore stack ptr    }
  332. {168} $8E/$16/>DosSs/    { MOV  SS,[DosSs]  ;SS:SP.  Clear in-use }
  333. {172} $80/$26/>Flg/$FD/  { AND  [Flg],0FDh  ;bit of Flg.          }
  334.  
  335. {177} $FB/           {PP2: STI              ;Enable interrupts.   }
  336. {178} $58/               { POP  AX          ;Restore AX.          }
  337. {179} $C3 );             { RET              ;Return.              }
  338. {180}
  339. END; {Asm.} {END corresponds to 12 bytes of code--used for storage}
  340.  
  341. PROCEDURE PopUpCode; {Interface between the BIOS intercept        }
  342. INTERRUPT;           {routines and your TSR function.             }
  343. CONST  BSeg = $0040;   VBiosOfs = $49;
  344. TYPE
  345.   VideoRecs = RECORD
  346.                 VideoMode                      : BYTE;
  347.                 NumbCol, ScreenSize, MemoryOfs : WORD;
  348.                 CursorArea      : ARRAY[0..7] OF WORD;
  349.                 CursorMode                     : WORD;
  350.                 CurrentPage                    : BYTE;
  351.                 VideoBoardAddr                 : WORD;
  352.                 CurrentMode, CurrentColor      : BYTE;
  353.               END;
  354. VAR
  355.   Regs             : Registers;
  356.   VideoRec         : VideoRecs;
  357.   KeyLock          : BYTE;
  358.   ScrnSeg, NumbChr : WORD;
  359. BEGIN
  360.   SwapVectors;                            {Set T.P. intr. vectors.}
  361.   Move( Ptr(BSeg,VBiosOfs)^, VideoRec,    {Get Video BIOS info.   }
  362.         SizeOf(VideoRec) );
  363.   WITH VideoRec, Regs DO BEGIN
  364.     IF (VideoMode > 7) OR                  {Abort pop up if unable}
  365.        (ScreenSize > BuffSize) THEN BEGIN  {to save screen image. }
  366.       SwapVectors;                         {Restore intr. vectors.}
  367.       Exit;
  368.     END;
  369.     KeyLock := Mem[BSeg:$0017];            {Save lock key states. }
  370.     IF VideoMode = 7 THEN ScrnSeg := $B000 {Save screen--supports }
  371.     ELSE ScrnSeg := $B800;                 {text, MGA & CGA modes.}
  372.     Move( PTR( ScrnSeg, MemoryOfs )^, Buffer, ScreenSize );
  373.     AX := InitVideo;                       {If in graphics mode,  }
  374.     IF (VideoMode >=4)                     {switch to text mode.  }
  375.        AND (VideoMode <= 6) THEN Intr( $10, Regs );
  376.     AX := $0500;                           {Select display page 0.}
  377.     Intr( $10, Regs );
  378.     CX := InitCMode;                       {Set cursor size.      }
  379.     AH := 1;
  380.     Intr( $10, Regs );
  381.  
  382.     TSRMode   := VideoMode;              {Fill global variables   }
  383.     TSRWidth  := NumbCol;                {with current information}
  384.     TSRPage   := CurrentPage;
  385.     TSRColumn := Succ( Lo( CursorArea[CurrentPage] ) );
  386.     TSRRow    := Succ( Hi( CursorArea[CurrentPage] ) );
  387.  
  388.     IF NpxFlag THEN                      {Save co-processor state.}
  389.       INLINE( $98/ $DD/$36/>NpxState );  {WAIT FSAVE [NpxState]   }
  390. {
  391. *** Call user's program and save return code--no. char. to insert.
  392. }
  393.     NumbChr := TheirFunc;
  394.     MemW[CSeg:InsNumb] := NumbChr;
  395.     IF NumbChr > 0 THEN BEGIN               {Have char. to insert.}
  396.       MemL[CSeg:InsChr] := LONGINT( TSRChrPtr );
  397.       Mem[CSeg:Flg]     := Mem[CSeg:Flg] OR $10;
  398.     END;
  399. {
  400. *** Pop TSR back down--Restore computer to previous state.
  401. }
  402.     IF NpxFlag THEN                      {Restore co-prcssr state.}
  403.       INLINE( $98/ $DD/$36/>NpxState );  {WAIT FSAVE [NpxState]   }
  404.  
  405.     Mem[BSeg:$17] :=                     {Restore key lock status.}
  406.       (Mem[BSeg:$17] AND $0F) OR (KeyLock AND $F0);
  407.  
  408.     IF Mem[BSeg:VBiosOfs] <> VideoMode THEN BEGIN
  409.       AX := VideoMode;                   {Restore video mode.     }
  410.       Intr( $10, Regs );
  411.     END;
  412.     AH := 1;  CX := CursorMode;          {Restore cursor size.    }
  413.     Intr( $10, Regs );
  414.     AH := 5;  AL := CurrentPage;         {Restore active page.    }
  415.     Intr( $10, Regs );
  416.     AH := 2;  BH := CurrentPage;         {Restore cursor positon. }
  417.     DX := CursorArea[CurrentPage];
  418.     Intr( $10, Regs );                   {Restore screen image.   }
  419.     Move( Buffer, PTR( ScrnSeg, MemoryOfs )^, ScreenSize );
  420.  
  421.     SwapVectors;                        {Restore non-T.P. vectors.}
  422.   END;
  423. END;  {PopUp.}
  424. {
  425. ***** Printer Functions:
  426. }
  427. FUNCTION PrinterStatus: BYTE;             {Returns status of LPT1.}
  428. { Definition of status byte bits (1 & 2 are not used), if set then:
  429.  Bit: -- 7 ---  ---- 6 ----  -- 5 ---  -- 4 ---  -- 3 --  --- 0 ---
  430.       Not busy  Acknowledge  No paper  Selected  I/O Err. Timed-out
  431. }
  432. VAR Regs  : Registers;
  433. BEGIN
  434.   WITH Regs DO BEGIN
  435.     AH := 2;  DX := 0;    {Load BIOS function and printer number. }
  436.     Intr( $17, Regs );    {Call BIOS printer services.            }
  437.     PrinterStatus := AH;  {Return with printer status byte.       }
  438.   END;
  439. END; {PrinterStatus.}
  440.  
  441. FUNCTION PrinterOkay: BOOLEAN;  {Returns TRUE if printer is okay. }
  442. VAR  S : BYTE;
  443. BEGIN
  444.   S := PrinterStatus;
  445.   IF ((S AND $10) <> 0) AND ((S AND $29) = 0) THEN
  446.     PrinterOkay := TRUE
  447.   ELSE PrinterOkay := FALSE;
  448. END;  {PrinterOkay.}
  449. {
  450. ***** Procedures to obtain contents of saved screen image.
  451. }
  452. PROCEDURE ScreenLine( Row: BYTE; VAR Line: LineWords;
  453.                                  VAR Words: BYTE );
  454. BEGIN
  455.   Words := 40;                        {Determine screen line size.}
  456.   IF TSRMode > 1 THEN  Words := Words*2;          {Get line's     }
  457.   Move( Buffer[Pred(Row)*Words], Line, Words*2 ); {characters and }
  458. END;  {ScreenLine.}                               {colors.        }
  459.  
  460. FUNCTION ScreenLineStr( Row: BYTE ): String80; {Returns just chars}
  461. VAR
  462.   Words, i   : BYTE;
  463.   LineWord   : LineWords;
  464.   Line       : String80;
  465. BEGIN
  466.   ScreenLine( Row, LineWord, Words );   {Get chars & attributes.  }
  467.   Line := '';                           {Move characters to string}
  468.   FOR i := 1 TO Words DO Insert( LineWord[i].C, Line, i );
  469.   ScreenLineStr := Line;
  470. END;  {ScreenString.}
  471. {
  472. ***** TSR Installation procedure.
  473. }
  474. PROCEDURE TSRInstall( TSRName: STRING; TSRFunc: WordFuncs;
  475.                       ShiftComb: BYTE; KeyChr: CHAR );
  476. CONST
  477.   ScanChr = '+1234567890++++QWERTYUIOP++++ASDFGHJKL+++++ZXCVBNM';
  478.   CombChr = 'RLCA"';
  479. VAR
  480.   PlistPtr         : ^STRING;
  481.   i, j, k          : WORD;
  482.   Regs             : Registers;
  483.   Comb, ScanCode   : BYTE;
  484. BEGIN
  485.   IF Ofs( Asm ) <> 0 THEN EXIT;           {Offset of Asm must be 0}
  486.   MemW[CSeg:StkSs]  := SSeg;              {Save pointer to top of }
  487.   MemW[CSeg:StkOfs] := Sptr + 562;        {TSR's stack.           }
  488.   MemL[CSeg:PopUp]  := LONGINT(@PopUpCode); {Save PopUpCode addr. }
  489.   TheirFunc         := TSRFunc;           {& their TSR func. addr.}
  490.   Writeln('Installing Stay-Resident program: ',TSRName );
  491. {
  492. *****  Save intercepted interrupt vectors: $09, $16, $21, $25, $26.
  493. }
  494.   GetIntVec( $09, POINTER( MemL[CSeg:Bios9] ) );
  495.   GetIntVec( $16, POINTER( MemL[CSeg:Bios16] ) );
  496.   GetIntVec( $21, POINTER( MemL[CSeg:Dos21] ) );
  497.   GetIntVec( $25, POINTER( MemL[CSeg:Dos25] ) );
  498.   GetIntVec( $26, POINTER( MemL[CSeg:Dos26] ) );
  499. {
  500. ***** Get equipment list and video mode.
  501. }
  502.   WITH Regs DO BEGIN
  503.     Intr( $11, Regs );                  {Check equipment list for }
  504.     NpxFlag := (AL AND 2) = 2;          {math co-processor.       }
  505.     AH      := 15;                      {Get current video mode   }
  506.     Intr( $10, Regs );                  {and save it for when TSR }
  507.     InitVideo := AL;                    {is activated.            }
  508.     AH := 3; BH := 0;                   {Get current cursor size  }
  509.     Intr( $10, Regs );                  {and save it for when TSR }
  510.     InitCMode := CX;                    {is activated.            }
  511.   END;  {WITH Regs}
  512. {
  513. ***** Get info. on buffer for saving screen image.
  514. }
  515.   BuffSize := SizeOf( Buffer );
  516.   TSRScrPtr := @Buffer;
  517. {
  518. *** Determine activation key combination.
  519. }
  520.   Comb := 0;  i := 1;                       {Create ptr to        }
  521.   PlistPtr := Ptr( PrefixSeg, $80 );        {parameter list.      }
  522.   WHILE i < Length( PlistPtr^ ) DO BEGIN    {Check for parameters.}
  523.     IF PlistPtr^[i] = '/' THEN BEGIN        {Process parameter.   }
  524.       Inc( i );
  525.       j := Pos( UpCase( PlistPtr^[i] ), CombChr );
  526.       IF (j > 0) AND (j < 5) THEN Comb := Comb OR (1 SHL Pred(j))
  527.       ELSE IF j <> 0 THEN BEGIN             {New activation char. }
  528.         Inc( i );   k := Succ( i );
  529.         IF i > Length(PlistPtr^) THEN KeyChr := #0
  530.         ELSE BEGIN
  531.           IF ((k <= Length(PlistPtr^)) AND (PlistPtr^[k] = '"'))
  532.              OR (PlistPtr^[i] <> '"') THEN KeyChr := PlistPtr^[i]
  533.           ELSE KeyChr := #0;
  534.         END;  {ELSE BEGIN}
  535.       END;  {ELSE IF ... BEGIN}
  536.     END; {IF PlistPtr^[i] = '/'}
  537.     Inc( i );
  538.   END;  {WHILE ...}
  539.   IF Comb = 0 THEN Comb := ShiftComb;  {Use default combination.  }
  540.   IF Comb = 0 THEN Comb := AltKey;     {No default, use [Alt] key.}
  541.   ScanCode := Pos( UpCase( KeyChr ), ScanChr );  {Convert char. to}
  542.   IF ScanCode < 2 THEN BEGIN                     {scan code.      }
  543.     ScanCode := 2;  KeyChr := '1';
  544.   END;
  545.   Mem[CSeg:Shft] := Comb;             {Store shift key combination}
  546.   Mem[CSeg:Key]  := ScanCode;         {and scan code.             }
  547. {
  548. *** Output an installation message:  Memory used & activation code.
  549. }
  550.   Writeln( 'Memory used is approximately ',
  551.    ( ($1000 + Seg(FreePtr^) - PrefixSeg)/64.0):7:1,' K (K=1024).');
  552.   Writeln(
  553. 'Activate program by pressing the following keys simultaneously:');
  554.   IF (Comb AND 1) <> 0 THEN Write(' [Right Shift]');
  555.   IF (Comb AND 2) <> 0 THEN Write(' [Left Shift]');
  556.   IF (Comb AND 4) <> 0 THEN Write(' [Ctrl]');
  557.   IF (Comb AND 8) <> 0 THEN Write(' [Alt]');
  558.   Writeln(' and "', KeyChr, '".');
  559. {
  560. *** Intercept orig. interrupt vectors; Then exit and stay-resident.
  561. }
  562.   SetIntVec( $21, Ptr( CSeg, Our21 ) );
  563.   SetIntVec( $25, Ptr( CSeg, Our25 ) );
  564.   SetIntVec( $26, Ptr( CSeg, Our26 ) );
  565.   SetIntVec( $16, Ptr( CSeg, Our16 ) );
  566.   SetIntVec( $09, Ptr( CSeg, Our09 ) );
  567.   SwapVectors;                           {Save turbo intr.vectors.}
  568.   MemW[CSeg:UnSafe] := 0;                {Allow TSR to pop up.    }
  569.   Keep( 0 );                             {Exit and stay-resident. }
  570. END;  {TSRInstall.}
  571. END.  {TSRUnit.}
  572.